package physics;

import display.Display;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

/**
 *
 * @author peter
 */
public class Engine extends Thread {
	private Display display_;
	private ArrayList<Shape> shapes_;
	private long currentTime_;

	public Engine(Display display)
	{
		display_ = display;
		shapes_ = new ArrayList<Shape>();
		display_.setShapes(shapes_);
		currentTime_ = System.nanoTime();
	}

	@Override
	public void run()
	{
		int fps = 100;
		int delay = 0;   // delay for 5 sec.
		int period = 1000/fps;  // repeat every sec.
		Timer timer = new Timer();

		timer.scheduleAtFixedRate(new TimerTask() {
			public void run()
			{
				// update entities
				for (Shape s : shapes_)
				;//	s.updatePosition(true, 2);

				//update gfx
				display_.repaint();
			}
		}, delay, period);
	}

	public void bad_time_based_run()
	{
		for (int i = 0; i < 10000; i++) {
			//input ...
			long newTime = System.nanoTime();
			float deltaTime = (float) (newTime - currentTime_) / 100000000.0f;
			currentTime_ = newTime;

			// update entities
			for (Shape s : shapes_)
				;//s.updatePosition(true, deltaTime);

			//update gfx
			updateDisplay();
		}
	}

	public void updateDisplay(){
		display_.repaint();
	}

	public void addShape(Shape s)
	{
		shapes_.add(s);
	}

	public void addShapes(ArrayList<Shape> sa)
	{
		shapes_ = sa;
		display_.setShapes(sa);
	}

	public void removeAllShapes()
	{
		shapes_ = new ArrayList<Shape>();
	}
}
